package oop.analyzer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import oop.analyzer.method.CollectMethod;
import oop.data.DataStore;
import oop.data.RowData;

/**
 * 分析方法工厂,通过组合分析方法产生分析链,和执行分析链产生最终分析结果.
 */
public class AnalyzerBuilder {
    private List<AnalyzerMethod> analyzerMethodLinks;
    
    private AnalyzerBuilder() {
    }
    
    public static AnalyzerBuilder createAnalyzerLinks() {
        AnalyzerBuilder analyzerBuilder = new AnalyzerBuilder();
        analyzerBuilder.analyzerMethodLinks = new ArrayList<AnalyzerMethod>();
        return analyzerBuilder;
    }
    public AnalyzerBuilder append(AnalyzerMethod analyzerMethod) {
        analyzerMethodLinks.add(analyzerMethod);
        return this;
    }
    
    public AnalyzerBuilder insert(AnalyzerMethod analyzerMethod, int index) {
        analyzerMethodLinks.add(index, analyzerMethod);
        return this;
    }
    
    /**
     * 分析链构造和执行分析方法链
    */
    public Map<String, DataStore<RowData>> analy(DataStore dataStore) {
        
        DataStore analy_result = null;
        
        for (int i = 0; i < analyzerMethodLinks.size(); i++) {
            analy_result = analyzerMethodLinks.get(i).execute(analy_result == null ? dataStore : analy_result);
        }
        //返回 标签-行数据 形式的map,方便输出处理
        Map<String, DataStore<RowData>> result = new CollectMethod().getMap(analy_result);
        return result;
    }
    
}
